package com.first.xiinn.action;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.first.core.web.action.BaseAction;
import com.first.xiinn.model.BookType;
import com.first.xiinn.service.BookTypeService;
import com.google.gson.Gson;

public class BookTypeAction extends BaseAction{
	  
	private BookType bookType;
	
	@Resource
	private BookTypeService bookTypeService;
	
	public BookTypeAction()
    {
    }

	public BookType getBookType() {
		return bookType;
	}

	public void setBookType(BookType bookType) {
		this.bookType = bookType;
	}
	
	 public String list()
	    {
	        String opt = getRequest().getParameter("opt");
	        StringBuffer buff = new StringBuffer();
	        if(StringUtils.isNotEmpty(opt))
	        {
	            buff.append("[");
	        }else
	        {
	            //buff.append((new StringBuilder("[{id:'0',text:'")).append(AppUtil.getCompanyName()).append("',expanded:true,children:[").toString());
	        	buff.append(new StringBuilder("[{id:'0',text:'书籍目录',expanded:true,children:[").toString());
	        }
	        List listParent = bookTypeService.findByParentId("0");
	        
	        BookType bookType;                                     
	        for(Iterator iterator = listParent.iterator(); iterator.hasNext(); )
	        {
	        	bookType = (BookType)iterator.next();
	        	buff.append((new StringBuilder("{id:'")).append(bookType.getBookCatalogId()).append("',text:'").append(bookType.getBookCatalogName()).append("',").toString());
	        	buff.append(findChild(bookType.getBookCatalogId()));
	        
	        }

	        if(!listParent.isEmpty())
	        {
	            buff.deleteCharAt(buff.length() - 1);
	        }
	        if(StringUtils.isNotEmpty(opt))
	        {
	            buff.append("]");
	        } else
	        {
	            buff.append("]}]");
	        }
	        System.out.println(buff.toString());
	        setJsonString(buff.toString());
	        return "success";
	    }
	 
	 
	 
	 public String findChild(String bookCalalogId)
	    {
	        StringBuffer buff = new StringBuffer("");
	        List list = bookTypeService.findByParentId(bookCalalogId);
	        if(list.size() == 0)
	        {
	            buff.append("leaf:true},");
	            return buff.toString();
	        }
	        buff.append("children:[");
	        BookType bookType;
	        for(Iterator iterator = list.iterator(); iterator.hasNext(); )
	        {
	        	bookType = (BookType)iterator.next();
	            buff.append((new StringBuilder("{id:'")).append(bookType.getBookCatalogId()).append("',text:'").append(bookType.getBookCatalogName()).append("',").toString());
	            buff.append(findChild(bookType.getBookCatalogId()));
	        }

	        buff.deleteCharAt(buff.length() - 1);
	        buff.append("]},");
	        return buff.toString();
	    }
	 
	 public String add()
	    {
		   if(bookType.getBookCatalogId().equals("")){
			   bookType.setBookCatalogId(null);
	        }
		    bookTypeService.save(this.bookType);
	        if(bookType != null){
	            setJsonString("{success:true}");
	        } else{
	            setJsonString("{success:false}");
	        }
	        return "success";
	    }
	 
	 public String detail()
	    {
		    String bookCalalogId = getRequest().getParameter("bookCalalogId");
	        setBookType((BookType)bookTypeService.get(bookCalalogId));
	        Gson gson = new Gson();
	        StringBuffer sb = new StringBuffer("{success:true,data:[");
	        sb.append(gson.toJson(bookType));
	        sb.append("]}");
	        setJsonString(sb.toString());
	        return "success";
	    }
	 
	 public String remove()
	    {
	        String bookCalalogId = getRequest().getParameter("bookCatalogId");
	        BookType bookType = (BookType)bookTypeService.get(bookCalalogId);
	        bookTypeService.remove(bookType);
	        setJsonString("{success:true}");
	        return "success";
	    }
	
	 public String comboxTree(){
	        String parentid = getRequest().getParameter("parentid");
	        StringBuffer buff = new StringBuffer();
	        buff.append("[");
	        List listParent = bookTypeService.findByParentId(parentid);
	        BookType bookType;                                     
	        for(Iterator iterator = listParent.iterator(); iterator.hasNext(); )
	        {
	        	bookType = (BookType)iterator.next();
	        	buff.append((new StringBuilder("{id:'")).append(bookType.getBookCatalogId()).append("',text:'").append(bookType.getBookCatalogName()).append("',").toString());
	        	buff.append(findChild(bookType.getBookCatalogId()));
	        
	        }
	        if(!listParent.isEmpty()) {
	            buff.deleteCharAt(buff.length() - 1);
	        }
	            buff.append("]");
	        System.out.println(buff.toString());
	        setJsonString(buff.toString());
	        return "success";
	    }
}


